Yacon.widget.Tree = function(conText) {
	this.rootKey = "";
	this.rootCaption = "";
	this.rootIco = "";
	this.dirHiddenIco = "";
	this.dirShowIco = "";
	this.leafIco = "";
	this.isEdit = false;
	this.isShow = false;
	this.isShowed = false;
	this.showKey = "";
	this.checkType = "";

	Yacon.apply(this, conText);
	this.create();

	return this.rootNode;
}

Yacon.widget.Tree.Extend(Yacon.widget.WinControl, {

	create : function() {
		if (!this.element)
			this.element = document.getElementById(this.elementID);

		if (!this.element)
			return;

		this.element.Control = this;

		this.rootKey = this.element.getAttribute("rootKey");
		this.rootCaption = this.element.getAttribute("rootCaption");
		this.checkType = this.element.getAttribute("checkType");

		this.rootIco = this.element.getAttribute("rootIco");
		this.dirHiddenIco = this.element.getAttribute("dirHiddenIco");
		this.dirShowIco = this.element.getAttribute("dirShowIco");
		this.leafIco = this.element.getAttribute("leafIco");
		this.parentKeyName = this.element.getAttribute("parentKeyName");
		this.fieldKeyName = this.element.getAttribute("fieldKeyName");
		this.fieldCaptionName = this.element.getAttribute("fieldCaptionName");
		this.fieldCountName = this.element.getAttribute("fieldCountName");
		this.dataSetID = this.element.getAttribute("dataSet");
		this.freshMethod = this.element.getAttribute("freshMethod");
		this.excuteMethod = this.element.getAttribute("excuteMethod");

		this.isEdit = Yacon.isTrue(this.element.getAttribute("isEdit"));
		this.isShow = Yacon.isTrue(this.element.getAttribute("isShow"));
		this.showKey = this.element.getAttribute("showKey");

		this.initialization();

		this.createPopupMenu(this.element.getAttribute("popupMenu"));

		addResizeElement(this.element);

		Yacon.addEvents(document.body, "contextmenu", Yacon.widget.PopupMenu.events.onBodyContextmenu);
	},
	initialization : function() {
		this.rootNode = new Yacon.widget.Tree.Node({
			caption : this.rootCaption,
			checkType : this.checkType
		});
		this.rootNode.level = 0;
		this.rootNode.tree = this;
		this.rootNode.setKey(this.rootKey);
		this.rootNode.setIcon(this.rootIco);

		this.element.appendChild(this.rootNode.li);

		if (!Yacon.isEmpty(this.dataSetID)) {
			this.dataSet = createDataset(this.dataSetID);
			if (!Yacon.isEmpty(this.freshMethod))
				this.dataSet.setRefreshMethod(this.freshMethod);
			if (!Yacon.isEmpty(this.excuteMethod))
				this.dataSet.setExcuteMethod(this.excuteMethod);

			this.dataSet.setAfterRefresh(this.setDataSet);

			this.rootNode.dataSet = this.dataSet;

			this.rootNode.refresh();
		}

		this.onresize();
	},

	onresize : function() {

		var pos = Yacon.getAbsPosition(this.element);
		var abttop = pos[1] + this.element.offsetHeight;

		var resizeval = 5;
		var btns = document.getElementsByTagName("span");
		for (var i = 0; i < btns.length; i++) {
			if (btns[i].elementType == "button") {
				resizeval = 30;
				break;
			}
		}

		if (abttop > document.body.clientHeight) {
			abttop = abttop + resizeval;
			this.element.style.height = (this.element.offsetHeight - (abttop - document.body.clientHeight)) + "px";

		} else {
			abttop = abttop + resizeval;
			this.element.style.height = (this.element.offsetHeight + (document.body.clientHeight - abttop)) + "px";
		}

		if (pos[2] < 180)
			this.element.style.width = 180 + "px";

		var tdParent = Yacon.getTD(this.element);
		if (tdParent != null || tdParent != undefined)
			tdParent.style.height = this.element.style.height;

		var frames = document.getElementsByTagName("iframe");
		for (var i = 0; i < frames.length; i++)
			frames[i].style.height = this.element.style.height;

	},
	/**
	 * 添加数据集
	 */
	setDataSet : function(dataSet) {
		if (!dataSet.node) {
			alert("请给数据集赋节点属性！");
			return;
		}
		dataSet.node.dataSet = dataSet;
		dataSet.node.waitforLoad = false;
		dataSet.node.removeChild();

		var record, treeNode;
		for (var i = 0; i < dataSet.getRecordCount(); i++) {
			record = dataSet.getRecord(i);
			treeNode = dataSet.node.addChild(record.getFieldValue(dataSet.node.tree.fieldKeyName), record.getFieldValue(dataSet.node.tree.fieldCaptionName));
			treeNode.setRecord(record);
		}
		dataSet.node.setExpanded(true);
		if (dataSet.node.firstChild)
			dataSet.node.tree.expandShow(dataSet.node.firstChild);
	},
	expandShow : function(node) {

		if (this.isShow && !this.isShowed) {
			if (!node.waitforLoad) {
				node.setSelected();
			} else {
				node.setExpanded(true);
			}
		}
	},
	createPopupMenu : function(menuStr) {
		if (menuStr) {
			var menuItems = eval(menuStr);
			if (menuItems) {
				this.popupMenu = new Yacon.widget.PopupMenu();
				for (var i = 0; i < menuItems.length; i++)
					this.popupMenu.add(menuItems[i].key, menuItems[i].text, menuItems[i].ico, menuItems[i].click);
			}
		}
	},
	getActvieEdit : function() {
		if (this.isEdit) {
			if (!this.treeEdit) {
				this.treeEdit = document.createElement("input");
				this.treeEdit.className = "active_editor";
				this.treeEdit.setAttribute("id", "treeEdit_text");
				Yacon.addEvents(this.treeEdit, "blur", Yacon.widget.Tree.Node.events.onTreeEditBlur);
				document.body.appendChild(this.treeEdit);

			}
			this.treeEdit.style.display = "none";
			return this.treeEdit;
		}

	},
	getSelectKey : function() {
		return this.rootNode.getSelectKey();
	},
	getSelectLevelKey : function(level) {
		return this.rootNode.getSelectLevelKey(level);
	},
	getSelectLevelCaption : function(level) {
		return this.rootNode.getSelectLevelCaption(level);
	},
	getSelectCaption : function() {
		return this.rootNode.getSelectCaption();
	},
	getSelectLeaf : function() {
		return this.rootNode.getSelectLeaf();
	}

})

Yacon.widget.Tree.Node = function(conText) {
	this.key = "";
	this.caption = "";
	this.checkType = "";
	this.loadChild = false;
	Yacon.apply(this, conText);
	this.create();

	this.setCaption(this.caption);
}

Yacon.widget.Tree.Node.Extend(Object, {

	/**
	 * 树形节点创建函数
	 */
	create : function() {
		this.li = document.createElement("li");
		this.li.align = "left";

		this.expImg = document.createElement("span");
		this.expImg.className = "expImg";
		this.expImg.innerText = "+";
		Yacon.addEvents(this.expImg, "click", Yacon.widget.Tree.Node.events.onImgClick);

		this.ischeck = false;
		this.checkbox = document.createElement("input");
		this.checkbox.className = "check";
		switch (this.checkType) {
		case "0":
			this.checkbox.style.display = "none";
			break;
		default:
			this.ischeck = true;
			this.checkbox.type = "checkbox";
			Yacon.addEvents(this.checkbox, "click", Yacon.widget.Tree.Node.events.onCheckClick);
		}

		this.imgIcon = document.createElement("img");
		this.imgIcon.className = "imgIco";
		Yacon.addEvents(this.imgIcon, "click", Yacon.widget.Tree.Node.events.onSpanClick);

		this.span = document.createElement("span");
		this.span.className = "text";
		Yacon.addEvents(this.span, "click", Yacon.widget.Tree.Node.events.onSpanClick);
		Yacon.addEvents(this.span, "mouseup", Yacon.widget.Tree.Node.events.onSpanMouseUp);
		Yacon.addEvents(this.span, "mouseover", Yacon.widget.Tree.Node.events.onSpanMouseOver);
		Yacon.addEvents(this.span, "mouseout", Yacon.widget.Tree.Node.events.onSpanMouseOut);

		this.ul = document.createElement("ul");
		this.ul.style.padding = "0px";
		this.ul.style.display = "none";
		this.ul.style.listStyleType = "none";
		this.ul.style.marginLeft = "15px";

		with (this.li) {
			appendChild(this.expImg);
			appendChild(this.checkbox);
			appendChild(this.imgIcon);
			appendChild(this.span);
			appendChild(this.ul);
		}
		this.li.node = this;
		this.level = 0;
		this.children = 0;
	},
	/**
	 * 添加子节点 参数：key 节点主键值，text 节点显示标题
	 */
	addChild : function(key, caption) {
		var node = new Yacon.widget.Tree.Node({
			caption : caption,
			checkType : this.tree.checkType
		});
		node.key = key;
		node.parent = this;
		node.tree = this.tree;
		node.type = "leaf";

		if (this.children > 0) {
			node.previous = this.lastChild;
			this.lastChild.next = node;
			this.lastChild = node;
		} else {
			this.firstChild = node;
			this.lastChild = node;
		}
		this.children++;

		node.level = this.level + 1;
		;
		node.setIcon(this.tree.leafIco);

		if (this != this.tree.rootNode)
			this.setIcon(this.tree.dirHiddenIco);

		if (this.tree.rootNode != this)
			this.type = "group";
		else
			this.type = "root";

		this.ul.appendChild(node.li);

		if (this.ischeck)
			node.checkbox.checked = this.checkbox.checked;

		this.expImg.style.visibility = "visible";
		return node;
	},
	/**
	 * 获取节点键值
	 */
	getKey : function() {
		return this.key;
	},
	/**
	 * 赋值节点键值 参数：key 节点键值
	 */
	setKey : function(key) {
		this.key = key;
	},
	/**
	 * 获取节点标题
	 */
	getCaption : function() {
		return this.caption;
	},
	/**
	 * 赋值节点标题 参数：text 节点显示标题
	 */
	setCaption : function(caption) {
		this.span.innerText = caption;
		this.span.title = caption;
		this.caption = caption;
	},
	/**
	 * 获取节点父结点
	 */
	getParent : function() {
		return this.parent;
	},
	/**
	 * 获取节点孩子数
	 * 
	 * @return
	 */
	getChildCount : function() {
		return this.children;
	},
	/**
	 * 获取节点下一个节点
	 */
	getNext : function() {
		return this.next;
	},
	/**
	 * 获取节点前一个节点
	 */
	getPrevious : function() {
		return this.previous;
	},
	/**
	 * 赋值图片
	 */
	setIcon : function(imgPath) {
		if (imgPath) {
			if (!imgPath.contains(contextPath))
				imgPath = contextPath + imgPath;

			this.imgIcon.setAttribute("src", imgPath);
			this.imgIcon.style.display = "inline";
		}
	},
	/**
	 * 清除节点
	 */
	removeChild : function() {
		while (this.children > 0) {
			this.firstChild.clear();
		}
		if (this != this.tree.rootNode) {
			this.setIcon(this.tree.leafIco);
			this.type = "leaf";
		}
		this.expImg.style.visibility = "hidden";
	},
	/**
	 * 清空节点属性
	 */
	clear : function() {

		/* 清除子节点 */
		this.removeChild();

		/* 清空自身信息 */
		if (this.previous)
			this.previous.next = this.next;
		if (this.next)
			this.next.previous = this.previous;

		with (this.parent) {
			with (this.li) {
				removeChild(this.expImg);
				removeChild(this.checkbox);
				removeChild(this.imgIcon);
				removeChild(this.span);
				removeChild(this.ul);
			}

			ul.removeChild(this.li);
			if (firstChild == this)
				firstChild = this.next;

			if (lastChild == this)
				lastChild = this.previous;

			children--;
			if (children == 0) {
				expImg.style.visibility = "hidden";
			}
		}

		this.NodeData = null;
		this.tree = null;
		this.parent = null;
		this.li.node = null;
		this.li = null;
		this.span = null
		this.ul = null
		this.img = null;
		this.checkbox = null;
		this.imgIcon = null;

		this.previous = null;
		this.next = null;
		this.firstChild = null;
		this.lastChild = null;
		this.frecord = null;
		this.dataSet = null;
		this.caption = "";
		this.key = "";
	},
	/**
	 * 获取节点记录
	 */
	getRecord : function() {
		return this.frecord;
	},
	/**
	 * 节点展开 、收缩
	 */
	setExpanded : function(isexp) {
		if (isexp) {
			/* 外部调用函数 */
			if (this.waitforLoad) {
				if (Yacon.isfireuserEvent("node_LoadChild"))
					Yacon.fireUserEvent("node_LoadChild", [ this ]);

				if (this.dataSet)
					this.refresh();
			}
			if (this.children > 0) {
				this.ul.style.display = "block";
				this.expImg.innerText = "-";

				if (this != this.tree.rootNode)
					this.setIcon(this.tree.dirShowIco);
			}

		} else {
			if (this != this.tree.rootNode)
				this.setIcon(this.tree.dirHiddenIco);

			this.ul.style.display = "none";
			this.expImg.innerText = "+";
		}

	},
	/**
	 * 节点选中
	 */
	setSelected : function() {
		this.actvieEdit();

		if (this.tree.selectedNode) {
			if (this.tree.selectedNode == this)
				return;

			if (this.tree.selectedNode.span) {
				this.tree.selectedNode.span.className = "text";
			}
		}

		this.tree.selectedNode = this;
		this.span.className = "activeNodeText";

		/* 外部调用函数 */
		if (Yacon.isfireuserEvent("node_click"))
			Yacon.fireUserEvent("node_click", [ this ]);

	},
	checkParent : function() {
		var pchild = this.parent;
		while (pchild) {
			if (pchild.type != "root") {
				if (pchild.getSelectKey() != "") {
					pchild.checkbox.checked = true;
				} else {
					pchild.checkbox.checked = false;
				}
			} else {
				var checkful = pchild.rootCheckful();
				pchild.checkbox.checked = checkful;
			}

			/* 递归 */
			pchild.checkParent();

			pchild = pchild.parent;
		}
	},
	rootCheckful : function() {
		var fnchild = this.firstChild;
		var checkroot = true;
		while (fnchild) {
			if (!fnchild.checkbox.checked) {
				checkroot = false;
				break;
			}
			if (checkroot) {
				checkroot = fnchild.rootCheckful();
				if (!checkroot) {
					checkroot = false;
					break;
				}
			}
			fnchild = fnchild.next;
		}
		return checkroot;
	},
	checkChild : function() {
		/* 首先展开 子节点 */
		// this.setExpanded(true);
		var nchild = this.firstChild;
		while (nchild) {
			nchild.checkbox.checked = this.checkbox.checked;
			/* 递归 */
			nchild.checkChild();

			nchild = nchild.next;
		}
	},
	actvieEdit : function() {
		if (this.tree.isEdit && this.tree.getActvieEdit()) {
			var isCellEdit = false;
			if (this.frecord && (this.frecord.getState().isEqual("insert") || this.frecord.getState().isEqual("edit")))
				isCellEdit = true;

			if (!isCellEdit)
				return;

			this.tree.treeEdit.node = this;
			this.tree.treeEdit.value = this.frecord.getFieldValue(this.tree.fieldCaptionName);

			var cellpost = Yacon.getAbsPosition(this.span);

			this.tree.treeEdit.style.left = (cellpost[0] - 1) + "px";
			this.tree.treeEdit.style.top = (cellpost[1] - 2) + "px";
			this.tree.treeEdit.style.width = 120 + "px";
			this.tree.treeEdit.style.display = "";
			this.tree.treeEdit.style.position = "absolute";

			this.tree.treeEdit.focus();
		}
	},
	setFieldValue : function(fieldValue) {
		if (document.all("treeEdit_text"))
			document.all("treeEdit_text").style.display = "none";

		if (!Yacon.isEmpty(fieldValue)) {
			this.setCaption(fieldValue);
			if (this.frecord && (this.frecord.getState().isEqual("insert") || this.frecord.getState().isEqual("edit")))
				this.frecord.setFieldValue(this.tree.fieldCaptionName, fieldValue);

			this.post();
		}
	},
	/**
	 * 赋值数据记录
	 */
	refresh : function() {
		if (this.dataSet) {
			this.dataSet.node = this;
			if (Yacon.isfireuserEvent("node_beforeRefresh"))
				Yacon.fireUserEvent("node_beforeRefresh", [ this ]);

			this.dataSet.addAttribute(this.tree.parentKeyName, this.getKey());
			this.dataSet.addAttribute("treeLevel", this.level);
			this.dataSet.setWhereField(this.tree.parentKeyName, this.getKey());
			this.dataSet.setWhereField("treeLevel", this.level);

			this.dataSet.refresh();
		}
	},
	/**
	 * 提交当前节点数据集
	 */
	post : function() {
		if (this.frecord.fDataSet) {
			this.frecord.fDataSet.post();
			this.tree.rootNode.refresh();
		}
	},
	/**
	 * 创建数据节点
	 */
	creatNode : function() {
		this.dataSet.createRecord();
		this.dataSet.addAttribute(this.tree.parentKeyName, this.getKey());
		this.dataSet.addAttribute("treeLevel", this.level);
		this.dataSet.setFieldValue(this.tree.parentKeyName, this.getKey());
		this.dataSet.setFieldValue("treeLevel", this.level);

		if (this.tree.isEdit) {
			this.dataSet.setFieldValue(this.tree.fieldCaptionName, "新节点");
			var childNode = this.addChild("", "新节点");
			childNode.setRecord(this.dataSet.getRecord());
			this.setExpanded(true);
		}

		return this.dataSet;
	},
	editNode : function() {

		this.parent.dataSet.getRecord(this.frecord);
		this.parent.dataSet.setState("edit");
		this.parent.dataSet.setFieldValue(this.tree.parentKeyName, this.parent.getKey());
		this.parent.dataSet.addAttribute(this.tree.parentKeyName, this.parent.getKey());
		this.parent.dataSet.addAttribute("treeLevel", this.level);
		return this.parent.dataSet;
	},
	delNode : function() {
		this.parent.dataSet.addAttribute(this.tree.parentKeyName, this.parent.getKey());
		this.parent.dataSet.addAttribute("treeLevel", this.level);

		if (this.tree.isEdit) {
			if (confirm("确定要删除数据吗？")) {
				this.parent.dataSet.getRecord(this.frecord);
				this.parent.dataSet.setState("delete");
				this.parent.dataSet.setFieldValue(this.tree.parentKeyName, this.parent.getKey());
				this.post();
			}
		} else {
			this.parent.dataSet.getRecord(this.frecord);
			this.parent.dataSet.setState("delete");
			this.parent.dataSet.setFieldValue(this.tree.parentKeyName, this.parent.getKey());
			this.post();
		}
	},
	/**
	 * 赋值数据记录
	 */
	setRecord : function(record) {
		this.frecord = record;
		this.dataSet = record.getChildDataSet();
		this.dataSet.node = this;

		for (var i = 0; i < record.length; i++) {
			var field = record.getField(i);
			this[field.fFieldName.toLowerCase()] = field.fFieldValue;
		}

		if ((this.parent && this.parent.loadChild) || Yacon.getInt(record.getFieldValue(this.tree.fieldCountName)) > 0) {
			this.waitforLoad = true;
			// this.addChild("","载入中...");
			this.setIcon(this.tree.dirHiddenIco);
			this.expImg.style.visibility = "visible";
		}
	},
	getSelectKey : function() {
		var keyStr = "";

		if (this.ischeck) {
			var nchild = this.firstChild;
			while (nchild) {
				if (nchild.checkbox.checked) {
					if (keyStr.isEqual("") && !Yacon.isEmpty(nchild.key)) {
						keyStr = nchild.key;
					} else if (!Yacon.isEmpty(nchild.key)) {
						keyStr += "," + nchild.key;
					}
				}

				var childKey = nchild.getSelectKey();
				if (!Yacon.isEmpty(childKey)) {
					if (keyStr.isEqual(""))
						keyStr = childKey;
					else
						keyStr += "," + childKey;
				}
				nchild = nchild.next;
			}
		} else if (this.tree.selectedNode)
			keyStr = this.tree.selectedNode.key;

		return keyStr;
	},
	getSelectLevelKey : function(level) {
		var keyStr = "";

		if (this.ischeck) {
			var nchild = this.firstChild;
			while (nchild) {
				if (nchild.checkbox.checked) {
					if(nchild.level == level){
						if (keyStr.isEqual("") && !Yacon.isEmpty(nchild.key)) {
							keyStr = nchild.key;
						} else if (!Yacon.isEmpty(nchild.key)) {
							keyStr += "," + nchild.key;
						}
					}
				}

				var childKey = nchild.getSelectLevelKey(level);
				if (!Yacon.isEmpty(childKey)) {
					if (keyStr.isEqual(""))
						keyStr = childKey;
					else
						keyStr += "," + childKey;
				}
				nchild = nchild.next;
			}
		} else if (this.tree.selectedNode)
			keyStr = this.tree.selectedNode.key;

		return keyStr;
	},
	getSelectLeaf : function() {
		var keyStr = "";
		if (this.ischeck) {
			var nchild = this.firstChild;
			while (nchild) {
				if (nchild.checkbox.checked && nchild.type.isEqual("leaf")) {
					if (keyStr.isEqual("") && !Yacon.isEmpty(nchild.key))
						keyStr = nchild.key;
					else if (!Yacon.isEmpty(nchild.key))
						keyStr += "," + nchild.key;

				}
				var childKey = nchild.getSelectLeaf();
				if (!Yacon.isEmpty(childKey)) {
					if (keyStr.isEqual(""))
						keyStr = childKey;
					else
						keyStr += "," + childKey;
				}

				nchild = nchild.next;
			}
		} else if (this.tree.selectedNode && this.tree.selectedNode.type.isEqual("leaf"))
			keyStr = this.tree.selectedNode.key;

		return keyStr;
	},
	getSelectCaption : function() {
		var captionStr = "";
		if (this.ischeck) {
			var nchild = this.firstChild;
			while (nchild) {
				if (nchild.checkbox.checked) {
					if (captionStr.isEqual("") && !Yacon.isEmpty(nchild.caption)) {
						captionStr = nchild.caption;
					} else if (captionStr.length > 0 && !Yacon.isEmpty(nchild.caption)) {
						captionStr += "," + nchild.caption;
					}
				}
				
				var childCaption=nchild.getSelectCaption();
				if(!Yacon.isEmpty(childCaption)){
					if (captionStr.isEqual("")) {
						captionStr = childCaption;
					} else {
						captionStr += "," + childCaption;
					}
				}
				nchild = nchild.next;
			}
		}else if (this.tree.selectedNode) {
			captionStr = this.tree.selectedNode.caption;
		}
		return captionStr;
	},
	getSelectLevelCaption : function(level) {
		var keyStr = "";

		if (this.ischeck) {
			var nchild = this.firstChild;
			while (nchild) {
				if (nchild.checkbox.checked) {
					if(nchild.level == level){
						if (keyStr.isEqual("") && !Yacon.isEmpty(nchild.caption)) {
							keyStr = nchild.caption;
						} else if (!Yacon.isEmpty(nchild.caption)) {
							keyStr += "," + nchild.caption;
						}
					}
				}

				var childKey = nchild.getSelectLevelCaption(level);
				if (!Yacon.isEmpty(childKey)) {
					if (keyStr.isEqual(""))
						keyStr = childKey;
					else
						keyStr += "," + childKey;
				}
				nchild = nchild.next;
			}
		} else if (this.tree.selectedNode)
			keyStr = this.tree.selectedNode.caption;

		return keyStr;
	},
	getSelKeyCaption : function() {
		return this.getSelectKey() + "#" + this.getSelectCaption();
	},
	getSelLeafCaption : function() {
		var captionStr = "";
		if (this.ischeck) {
			var nchild = this.firstChild;
			while (nchild) {
				if (nchild.checkbox.checked && nchild.type.isEqual("leaf")) {
					if (captionStr.isEqual("") && !Yacon.isEmpty(nchild.caption)) {
						captionStr = nchild.caption;
					} else if (captionStr.length > 0 && !Yacon.isEmpty(nchild.caption)) {
						captionStr += "," + nchild.caption;
					}
				}
				var childCaption = nchild.getSelLeafCaption();
				if (!Yacon.isEmpty(childCaption)) {
					if (captionStr.isEqual("")) {
						captionStr = childCaption;
					} else {
						captionStr += "," + childCaption;
					}
				}
				nchild = nchild.next;
			}
		} else if (this.tree.selectedNode && this.tree.selectedNode.type.isEqual("leaf")) {
			captionStr = this.tree.selectedNode.caption;
		}
		return captionStr;
	},
	getSelLeafCaptionAndKey : function() {
		return this.getSelectLeaf() + "#" + this.getSelLeafCaption();
	}
})

Yacon.widget.Tree.Node.events = {
	onImgClick : function() {
		var nodeImg = event.srcElement;
		while (!nodeImg.node)
			nodeImg = nodeImg.parentElement;

		if (nodeImg) {
			nodeImg = nodeImg.node;
			nodeImg.setExpanded(nodeImg.ul.style.display == "none");
		}
	},
	onCheckClick : function() {
		var nodeCheck = event.srcElement;
		while (!nodeCheck.node)
			nodeCheck = nodeCheck.parentElement;

		if (nodeCheck) {
			nodeCheck.node.checkChild();
			nodeCheck.node.checkParent();
			nodeCheck.node.setExpanded(true);
			/*
			 * var node = nodeCheck.node ; while(node){ node.setExpanded(true);
			 * //alert(node.firstChild); node = node.firstChild ; }
			 */

		}

	},
	onSpanClick : function() {
		var span = event.srcElement;
		while (!span.node)
			span = span.parentElement;

		if (span) {
			span.node.setExpanded(span.node.ul.style.display == "none");
			span.node.setSelected();

			/* 外部调用函数 */
			// if (Yacon.isfireuserEvent("node_click"))
			// Yacon.fireUserEvent("node_click",[span.node]);
		}
	},
	onSpanMouseUp : function() {
		
		if (event.button == 2) {
			var span;
			//IE 与 火狐兼容
			if(window.isfirefox){
				span = event.srcElement ? event.srcElement : event.target;
			}else{
				span = event.srcElement;
			}
			
			//循环处理
			while (!span.node){
				if(window.isIE){
					span = span.parentElement;
				}else{
					span = span.parentNode;
				}
			}		

			if (span.node.tree.popupMenu) {
				span.node.tree.popupMenu.node = span.node;
				//通用属性 - 兼容IE与火狐
				if(window.isfirefox){
					span.node.tree.popupMenu.show(event.clientX, event.clientY);
				}else{
					span.node.tree.popupMenu.show(event.x, event.y);
				}				
			}
			/* 外部调用函数 */
			if (Yacon.isfireuserEvent("node_RightClick"))
				Yacon.fireUserEvent("node_RightClick", [ span.node ]);

		}
	},
	onSpanMouseOver : function() {
		if (event.srcElement.className != "activeNodeText")
			event.srcElement.className = "moveNodeText";
	},
	onSpanMouseOut : function() {
		if (event.srcElement.className != "activeNodeText")
			event.srcElement.className = "text";
	},
	onTreeEditBlur : function() {
		if (event.srcElement.node)
			event.srcElement.node.setFieldValue(event.srcElement.value);
	}
}

function tree_menu_Click(meunItem) {
	if (!meunItem || !meunItem.node || !meunItem.node.tree.isEdit)
		return;

	switch (meunItem.key) {
	case "add":
		meunItem.node.creatNode();
		break;
	case "edit":
		meunItem.node.editNode();
		break;

	case "del":
		meunItem.node.delNode();
		break;
	}
}

